<?php
class sql {
	public $db;
	private $result;
	public $row;
	public $last_result;
	private $last_query;

	public function __construct($server, $user, $pass, $db) {

		$this->db = new mysqli($server, $user, $pass, $db);

		if (mysqli_connect_errno()) {
   			printf("We're sorry, we are currently undergoing needed maintenance.");
   			exit();
		}

		$this->db->set_charset("utf8");
	}

	public function last_query(){
		return $this->last_query;
	}

	public function affected_rows() {
		return $this->db->affected_rows;
	}

	public function error() {
		return $this->db->error;
	}

	public function __destruct() {
		$this->db->close();
	}

	public function closeResult() {
		$this->result->close();
	}

	public function killThread($tid) {
		$this->db->kill($tid);
	}

	public function lastID() {
		return $this->db->insert_id;
	}

	public function real_escape_string($str){
		return $this->db->real_escape_string($str);
	}


	public function multi_query($query) {
		$this->last_query = $query;
		$this->db->multi_query($query);
	}

	public function num_rows() {
		return $this->result->num_rows;
	}

	public function query($query) {
		$this->last_query = $query;
		$this->result = $this->db->query($query);

		if($this->result)
			return $this->result;
		else
			return false;
	}

	public function fetch_assoc($result = false)
	{
		$this->last_result = null;

		if($result)
			$this->result = $result;

		if(!$this->result)
			return false;

		while ($this->row = $this->result->fetch_assoc())
		{
		    $this->last_result[] = $this->row;
		}

		$this->result->free();

		return $this->last_result;
	}

	public function fetch_row($result = false)
	{
		$this->last_result = null;

		if($result)
			$this->result = $result;

		if(!$this->result)
			return false;

		while ($this->row = $this->result->fetch_row())
		{
		    $this->last_result[] = $this->row;
		}

		$this->result->free();

		return $this->last_result;
	}

	public function fetch_field($field){
		$this->last_result = null;

		while($this->row = $this->result->fetch_assoc()){
			$this->last_result[] = $this->row[$field];
		}

		$this->result->free();

		return $this->last_result;
	}


	public function array_to_insert($table, $array, $duplicate = false){

		if(!is_array($array))
			return false;

		$fields = "";
		$values = "";
		$update = "";

		foreach($array as $key => $value){
			if(!empty($fields))
				$fields .= ", ";
			if(!empty($values))
				$values .= ", ";
			if(!empty($update))
				$update .= ", ";

			$value = $this->real_escape_string($value);

			$fields .= "`{$key}`";
			$values .= "'{$value}'";
			$update .= "`{$key}` = '{$value}'";
		}


		$sql = "INSERT INTO {$table} ({$fields}) VALUES ({$values})";
		if ($duplicate){
			$sql .= " ON DUPLICATE KEY UPDATE {$update}";
			$sql .= ($duplicate !== true) ? $duplicate : '';
		}

		$this->result = $this->db->query($sql);
		$this->last_query = $sql;
		return $this->result;
	}

	public function select_array($table, $arrayOfClause, $keyAll = false, $clause = 'and')
	{
		if(empty($table) or !is_array($arrayOfClause))
			return false;

		$query = "SELECT * FROM `{$table}` WHERE ";

		$sql = '';

		foreach($arrayOfClause as $key=>$item)
		{
			$keythis = (!$keyAll) ? $key : $keyAll;

			if(!empty($sql))
				$sql .= " {$clause} ";
			$sql .= "`{$keythis}` = '{$item}'";
		}

		$this->last_query = $query . $sql;
		$this->result = $this->db->query($query . $sql);

		return $this->result;
	}

	public function update_array($table, $arrayOfRows, $where_clause) {
		if(empty($table) or !is_array($arrayOfRows) or !is_array($where_clause))
			return false;

		$query = "UPDATE `{$table}` SET ";
		$sql = '';
		foreach($arrayOfRows as $key=>$item)
		{
			if(in_array($key, $where_clause))
				continue;
			$item = $this->real_escape_string($item);
			if(!empty($sql))
				$sql .= ", ";
			$sql .= "`{$key}` = '{$item}'";
		}

		$clause = '';

		foreach ($where_clause as $item) {

			if(!empty($clause))
				$clause .= " and ";
			$clause .= "`{$item}` = '{$arrayOfRows[$item]}'";

		}

		if(!empty($clause)){
			$sql .= " WHERE {$clause} ";
		}

		$this->last_query = $query . $sql;
		$this->result = $this->db->query($query . $sql);

		return $this->result;

	}

	public function delete_array($table, $arrayOfClause, $clause = 'and'){
		if(empty($table) or !is_array($arrayOfClause))
			return false;

		$query = "DELETE FROM `{$table}` WHERE ";
		$sql = '';
		foreach($arrayOfClause as $key=>$item)
		{
			if(!empty($sql))
				$sql .= " {$clause} ";
			$sql .= "`{$key}` = '{$item}'";
		}


		$this->last_query = $query . $sql;
		$this->result = $this->db->query($query . $sql);

		return $this->result;
	}
}
?>